package com.lzy.system.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzy.conf.JSONResult;
import com.lzy.system.business.dto.SysBusEntityAddDTO;
import com.lzy.system.business.dto.SysBusEntityUpdateDTO;
import com.lzy.system.business.dto.convert.SysBusEntityConvert;
import com.lzy.system.business.entity.SysBusEntity;
import com.lzy.system.business.service.ISysBusEntityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * <p>
 * 存储表 前端控制器
 * </p>
 *
 * @author lcc
 * @since 2024-01-21
 */
@RestController
@RequestMapping("/business/sys-bus-entity")
@Api(tags = "存储表")
public class SysBusEntityController {
    private ISysBusEntityService iSysBusEntityService;
    private SysBusEntityConvert sysBusEntityConvert;

    @Autowired
    public SysBusEntityController(ISysBusEntityService iSysBusEntityService, SysBusEntityConvert sysBusEntityConvert) {
        this.iSysBusEntityService = iSysBusEntityService;
        this.sysBusEntityConvert = sysBusEntityConvert;
    }

    @RequestMapping(value = "/getList", method = RequestMethod.POST)
    @ApiOperation("获取存储表 列表")
    public JSONResult getList(
            @ApiParam("每页显示行数") @RequestParam(name = "limit", required = true, defaultValue = "10") int limit,
            @ApiParam("当前页数") @RequestParam(name = "page", required = true, defaultValue = "1") int page
    ) {
        IPage<SysBusEntity> page_obj = iSysBusEntityService.page(new Page<SysBusEntity>(page, limit));
        return JSONResult.ok(page_obj);
    }

    @RequestMapping(value = "/getListAll", method = RequestMethod.POST)
    @ApiOperation("获取存储表 全部列表")
    public JSONResult getListAll(
            @ApiParam("busId") @RequestParam(name = "busId", required = true) String busId

    ) {
        return JSONResult.ok(iSysBusEntityService.list(new QueryWrapper(){{
            eq("bus_id", busId);
        }}));
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation("添加存储表")
    public JSONResult add(
            @Valid @ApiParam("存储表") @RequestBody @Validated SysBusEntityAddDTO data
    ) {
        iSysBusEntityService.saveEntity(data);
        return JSONResult.ok();
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation("修改存储表")
    public JSONResult update(
            @ApiParam("存储表") @RequestBody @Validated SysBusEntityUpdateDTO data
    ) {
        iSysBusEntityService.updateEntity(data);
        return JSONResult.ok();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation("删除存储表")
    public JSONResult delete(
            @ApiParam("ids") @RequestBody @Validated List<String> ids
    ) {
        iSysBusEntityService.deleteEntity(ids);
        return JSONResult.ok();
    }

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation("获取存储表详情")
    public JSONResult get(
            @ApiParam("id") @PathVariable(value = "id") @NotBlank(message = "id不能为空") String id
    ) {
        return JSONResult.ok(iSysBusEntityService.getById(id));
    }
}
